package ita.class1.group2.supermarket.administrator.listener;

import ita.class1.group2.supermarket.administrator.pojo.AdForm;
import ita.class1.group2.supermarket.administrator.service.AdFormService;
import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.util.JsonUtil;
import ita.class1.group2.supermarket.common.util.UUIDUtils;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.sql.Date;

@Component
public class ADFormListener implements MessageListener {
    @Autowired
    AdFormService adService;

    @Override
    public void onMessage(Message message) {
        try {
            String msg = ((TextMessage) message).getText();
            AdForm adForm = JsonUtil.json2pojo(msg, AdForm.class);
            adForm.setId(UUIDUtils.getUUID());
            adForm.setCreateTime(new Date(System.currentTimeMillis()));
            adForm.setUpdatetime(new Date(System.currentTimeMillis()).getTime());
            adService.saveAddForm(adForm);
            message.acknowledge();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
